﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using UDS.Components;

namespace UDS.Entity
{
    public class MailFullInfo
    {
        #region properties
        /// <summary>
        /// The identifier of the mail.
        /// </summary>
        public int ID
        {
            get;
            set;
        }

        public MailFolderType FolderType
        {
            get;
            set;
        }

        public MailImportance Importance
        {
            get;
            set;
        }

        public int ClassID
        {
            get;
            set;
        }

        public string ClassName
        {
            get;
            set;
        }

        public string Sender
        {
            get;
            set;
        }

        public string SenderRealName
        {
            get;
            set;
        }

        public DateTime TimeSent
        {
            get;
            set;
        }

        public string Subject
        {
            get;
            set;
        }

        public string Body
        {
            get;
            set;
        }

        public bool IsNew
        {
            get;
            set;
        }

        public string TOList
        {
            get;
            set;
        }

        private string toListRealName;
        private string ccListRealName;

        /// <summary>
        /// Get the real name of the receivers.
        /// </summary>
        public string TOList_RealName
        {
            get
            {
                if (toListRealName == null)
                {
                    Interlocked.CompareExchange(ref toListRealName, Staff.GetRealNameForStaffList(TOList), null);
                }

                return toListRealName;
            }
        }

        public string CCList
        {
            get;
            set;
        }

        /// <summary>
        /// Get the real name of the CC list.
        /// </summary>
        public string CCList_RealName
        {
            get
            {
                if (ccListRealName == null)
                {
                    Interlocked.CompareExchange(ref ccListRealName, Staff.GetRealNameForStaffList(CCList), null);
                }

                return ccListRealName;
            }
        }

        public string BCCList
        {
            get;
            set;
        }

        public IList<MailAttachmentInfo> Attachments
        {
            get;
            set;
        }
        #endregion

        #region methods

        internal static MailFullInfo ReadMailInfo(IDataReader dr)
        {
            MailFullInfo m = new MailFullInfo();
            m.ID = Convert.ToInt32(dr["MailID"]);
            m.ClassID = SQLUtil.ReadInt32(dr["ClassID"], -1);
            m.ClassName = Convert.ToString(dr["ClassName"]);
            m.Sender = Convert.ToString(dr["Sender"]);
            m.SenderRealName = Convert.ToString(dr["SenderRealName"]);
            m.TimeSent = Convert.ToDateTime(dr["TimeSent"]);
            m.Subject = Convert.ToString(dr["Subject"]);
            m.Body = Convert.ToString(dr["Body"]);
            m.IsNew = !Convert.ToBoolean(dr["ReadFlag"]);
            m.TOList = Convert.ToString(dr["TOList"]);
            m.CCList = Convert.ToString(dr["CCList"]);
            m.BCCList = Convert.ToString(dr["BCCList"]);
            m.FolderType = (MailFolderType)SQLUtil.ReadInt32(dr["FolderType"], (int)MailFolderType.Index);
            m.Importance = (MailImportance)SQLUtil.ReadInt32(dr["Importance"], (int)MailImportance.Normal);

            // Read attachments
            if (dr.NextResult())
            {
                List<MailAttachmentInfo> attachments = new List<MailAttachmentInfo>();
                while (dr.Read())
                {
                    attachments.Add(MailAttachmentInfo.ReadAttachmentInfo(dr));
                }
                m.Attachments = attachments.AsReadOnly();
            }

            return m;
        }
        #endregion
    }
}
